<?php
// +----------------------------------------------------------------------
// | 青航系统 [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2023-永久 https://www.feifeiyu.club/ All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青航-不吃芒果 <1716892803@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\exception\Jump;
use think\facade\View;
use app\admin\validate\Member_validate;
use app\admin\model\Point as PointModel;
class Member extends  Common{
    
    // 数据列表
    public function index() {
        $list = D('member')->order('userid DESC')->paginate(15);
    	return view('member/index',['list'=>$list]);
    }
    
    
    // 添加数据
    public function add() {
        $group = D('group')->select();
        if (request()->isPost()) {
            // 获取全部参数
    	    $postField = 'username,password,nickname,email,vip,overduedate,groupid,status';
			$data = request()->only(explode(',',$postField),'post');
    	    // 判断是否选择了vip会员
    		if($data['vip'] ==1 and !$data['overduedate'])
    		Jump::error('过期时间不能为空!'); 
    		 // 加载验证器
    	    $validate = new Member_validate();
    	    
            if (!$validate->check($data)) {
               Jump::error($validate->getError());
            }
            
            //  验证是否有数据
            $member = D('member')->where('username',$data['username'])->value('username');
    		if($member) Jump::error('用户名已存在!'); 
            $email =  D('member')->where('email',$data['email'])->value('email');
    		if($email) Jump::error('邮箱已存在!');
    		
            $data['overduedate'] = strtotime($data['overduedate']);
            $data['regdate'] = time();
            $data['password'] = password($data['password']);
            $data['regip'] = getip();
            
            $data['sex'] = '';
            $data['qq'] = '';
            $data['phone'] = '';
            $data['userpic'] = '';
            $data['birthday'] = '';
            $data['area'] = '';
            $data['motto'] = '';
            $data['cover'] = '';
            $data['openid'] = '';
            $r = D('member')->insert($data);
           
    		// 判断是否执行成功
            if($r){
    		   Jump::success("添加成功!");
    		}
    		else {
    		   Jump::error('添加错误!!');
    		}
    		
    	}
    	sleep(1);
    	return view('member/member_add',['group'=>$group]);
    }
    
    
    
    
    // 修改数据
    public function edit() {
        // 	输出会员等级类型
    	$group = D('group')->select();
       // 	获取网页中的userID值
    	$userid = request()->param('userid');
       // 	查询对应的信息
    	$find = D('member')->where('userid',$userid)->find();
        if (request()->isPost()) {
    		// 获取全部参数
    	    $postField = 'userid,username,password,nickname,email,vip,overduedate,groupid,status';
			$data = request()->only(explode(',',$postField),'post');
    	    // 判断是否选择了vip会员
    		if($data['vip'] ==1 and !$data['overduedate'])
    		Jump::error('过期时间不能为空!'); 
    		
    		$data['overduedate'] = strtotime($data['overduedate']);
            if($data['password']){
               $data['password']= password($data['password']);
            }
            else{
               unset($data['password']);
            }
            $r = D('member')->where('userid', $data['userid'])->update($data);
            
            
    		if($r){
    		    Jump::success('更新成功!'); 
    		}
    		else {
    		    Jump::error('更新失败,数据未改动!'); 
    		}
    		
    	}
    	sleep(1);
    	return view('member/member_edit',['group'=>$group,'find'=>$find]);
    }
    
    
    // 积分金额操作
	public function recharge(){ 
	    $list = D('member')->field('username')->order('userid DESC')->select();
		if (request()->isPost()) {
		    if(input('post.money')<0) Jump::error('数量不能为负数!');
			$username = input('post.username');
			$userinfo = D('member')->field('userid,email')->where(['username'=>$username])->find();
			if($userinfo){
				if(input('post.unit')) {
					PointModel::point_add(input('post.type'), floatval(input('post.money')), 4, $userinfo['userid'], $username, 0, input('post.remarks'), Session('adminname'), false);
				}else{
					PointModel::point_spend(input('post.type'), floatval(input('post.money')), 4, $userinfo['userid'], $username, input('post.remarks'));
				}
				Jump::success('操作成功!');
			}else{
				Jump::error('用户错误!'); 
			}
		    
		}
		sleep(1);
    	return view('member/member_recharge',['member'=>$list]);
	}    
    
    
    
    // 多选删除
    public function dels() {
    	if (request()->isPost()) {
    	    // 判断是否获取到值
    		if(!input('param.fx'))Jump::error('没有选择项目!!');
    		// 再循环提交过来的值
    		foreach(input('param.fx') as $id) {
               $r = D('member')->where('userid',$id)->delete();  
            }
         }
    	// 判断是否执行成功
    	if($r) {
    		Jump::success('删除成功!');
    	} else {
    		Jump::error('删除失败!');
    	}
    }    
    
    
    
    // 单选删除
    public function del() {
    	if (request()->isPost()) {
    	   // 获取当前网页的参数ID
    		$userid = request()->param('userid');
    		if(empty($userid) ) {Jump::error('ID不存在!');}
            if($data = D('member')->where('userid',$userid)->delete()){
    		  Jump::success('删除成功!');    
    		}
    		else{
    		  Jump::error('删除失败!');
    		}
    		
    	}
    }
    
    
    
}